<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Kernel\Traits\DaoTrait;
use App\Model\UserBusinessFav;
use App\Model\UserFav;

/**
 * 用户店铺收藏 Dao
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Dao
 */
class UserBusinessFavDao extends Dao
{
    use DaoTrait;

    /**
     * @inheritDoc
     */
    public function model(): string
    {
        return UserBusinessFav::class;
    }

    /**
     * 根据条件获取收藏个数
     *
     * @param array $map
     * @return int
     */
    public function count(array $map = [])
    {
        return UserBusinessFav::query()->where(function($query) use ($map) { return queryWhere($query, $map); })->count();
    }

    /**
     * 根据条件判获取收藏
     *
     * @param array $map
     * @return mixed
     */
    public function first(array $map = [])
    {
        return UserBusinessFav::query()->where(function($query) use ($map) { return queryWhere($query, $map); })->first();
    }

    /**
     * 列表
     *
     * @param array $map
     * @return mixed
     */
    public function get(array $map = [])
    {
        return UserBusinessFav::query()
            ->with('business:id,name,logo')
            ->withCount('businessFav')
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->orderByDesc('time')
            ->orderBy('id')
            ->select('id', 'business_id', 'status', 'time')
            ->paginate(10);
    }

    /**
     * 创建收藏
     *
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        return UserBusinessFav::query()->create($data);
    }

    /**
     * 删除
     *
     * @param array $map
     * @return int
     */
    public function delete(array $map = [])
    {
        return UserBusinessFav::query()->where(function($query) use ($map) {
            return queryWhere($query, $map);
        })->delete();
    }
}